<?php
class NOApi {
    private $debug = true;
    private $db;
    public function __construct($db) {
        $this->db = $db;
    }

    /**
     *
     * Method that handles electricity supplier change for a client
     * @param Client_NO2_SupplierChange $notifyOfClientESChangeRequest
     * @return SupplierChangeResponse
     */
    public function notifyOfClientESChange($notifyOfClientESChangeRequest) {
        if ($this->debug) {
            Logger::log('NOApi::notifyOfClientESChange', $notifyOfClientESChangeRequest);
        }

        $res = new Client_NO2_SupplierChangeResponse();
        $res->resultCode = 0;

        $validator = new Validator_NotifyOfClientESChange($this->db);
        if ($validator->isValid($notifyOfClientESChangeRequest)) {
            $es = new DB_EnergySupplier($this->db);
            if ($notifyOfClientESChangeRequest->oldES) {
                $es->save($notifyOfClientESChangeRequest->oldES);
            }
            $es->save($notifyOfClientESChangeRequest->newES);
            $client = new DB_Client($this->db);
            $client->save($notifyOfClientESChangeRequest->client);
            $clientESBind = new DB_ClientEnergySupplierBind($this->db);
            $clientESBind->save($notifyOfClientESChangeRequest);
            // add jobs
            $job = new Queue($this->db);
            // add/change client in portal
            $job->add('Portal_Client', 'save', new Portal_Client($notifyOfClientESChangeRequest->client->id, $notifyOfClientESChangeRequest->newES->id));

            $clientData = explode('.', $notifyOfClientESChangeRequest->client->id);
            $NOId = SERVICE_NO_ID;
            $areaId = $clientData[1];
            $facilityId = $clientData[2];

            if ($notifyOfClientESChangeRequest->oldES) {
                // send termination info to old ES
                if ($notifyOfClientESChangeRequest->oldES->id == 'T2ES1') {
                    $date = date('Y-m-d');
                    $supplyTerminationMessage = new Client_ES1_supplyTerminationMessage($areaId, $date, $facilityId, $NOId);
                    $sendSupplyTerminationMessage = new Client_ES1_SendSupplyTerminationMessage($supplyTerminationMessage);
                    $job->add('Client_ES1', 'sendSupplyTermination', $sendSupplyTerminationMessage);
                } else if ($notifyOfClientESChangeRequest->oldES->id == 'T2ES2') {
                    $clientContract = new Client_ES2_clientContractMessage();
                    $submitEndContract = new Client_ES2_submitEndContract($clientContract);
                    $job->add('Client_ES2', 'sendSupplyTermination', $submitEndContract);
                }
                // send metering task request to portal
                $job->add('Portal_Task', 'save', new Portal_Task($notifyOfClientESChangeRequest->client->id));
            }

            // notify new ES about the result
            if ($notifyOfClientESChangeRequest->newES->id == 'T2ES1') {
                $accepted = (boolean)(rand(0, 1)); // random true or false for accepting
                $supplierChangeReplyMessage = new Client_ES1_supplierChangeReplyMessage($accepted, $areaId, $facilityId, $NOId);
                $supplierChangeReplay = new Client_ES1_SupplierChangeReply($supplierChangeReplyMessage);
                $job->add('Client_ES1', 'sendSupplierChangeReplay', $supplierChangeReplay);
            } else if ($notifyOfClientESChangeRequest->newES->id == 'T2ES2') {
                // no such method available
            }
        } else {
            $res->resultCode = $validator->error;
        }
        return $res;
    }

    /**
     *
     * Method that handles quality series event
     * @param $qualitySeriesRequest
     * @return QualitySeriesResponse
     */
    public function qualitySeries($qualitySeriesRequest) {
        if ($this->debug) {
            Logger::log('NOApi::qualitySeries', $qualitySeriesRequest);
        }

        $res = new Client_NO2_QualitySeriesResponse();
        $res->resultCode = 0;

        $validator = new Validator_QualitySeries($this->db);
        if ($validator->isValid($qualitySeriesRequest)) {
            // save quality series object
        } else {
            $res->resultCode = $validator->error;
        }

        return $res;
    }

    /**
     *
     * Method that handles load profile event
     * @param $loadProfileRequest
     * @return LoadProfileResponse
     */
    public function loadProfile($loadProfileRequest) {
        if ($this->debug) {
            Logger::log('NOApi::loadProfile', $loadProfileRequest);
        }
        $res = new Client_NO2_LoadProfileResponse();
        $res->resultCode = 0;

        $validator = new Validator_LoadProfile($this->db);
        if ($validator->isValid($loadProfileRequest)) {
            // save the load profile object
        } else {
            $res->resultCode = $validator->error;
        }

        return $res;
    }
}
